package com.citrinus.feedme;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.citrinus.feedme.list.RestaurantsListAdapter;
import com.citrinus.feedme.listener.MenuItemClickListener;
import com.citrinus.feedme.places.Place;
import com.google.android.gms.internal.p;
import com.google.android.gms.maps.model.LatLng;

public class MenuFragment extends ListFragment {
	private static final String TAG = MenuFragment.class.getSimpleName();

	private View rootView;
	private List<Place> places;
	private RestaurantsListAdapter adapter;
	private MenuItemClickListener itemClickListener;

	public MenuFragment() {
		places = new ArrayList<Place>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		rootView = inflater.inflate(R.layout.fragment_menu, container, false);
		TextView emptyView = (TextView) rootView.findViewById(android.R.id.empty);
		emptyView.setTypeface(App.getRobot());
		return rootView;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		itemClickListener.menuItemClick(places.get(position));
	}

	public void updateData(LatLng point) {
		places.clear();
		places.addAll(App.instance.getDb().getNearPlaces(point));
		if (adapter == null) {
			adapter = new RestaurantsListAdapter(getActivity(), R.layout.row, places);
			setListAdapter(adapter);
		}
		adapter.notifyDataSetChanged();
	}

	public void setMenuItemClickListener(MenuItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
	}
}
